package com.ccut.lc.controller;

import com.ccut.lc.model.Order;
import com.ccut.lc.model.Product;
import com.ccut.lc.model.QueryVo;
import com.ccut.lc.model.User;
import com.ccut.lc.service.IUserService;
import com.ccut.lc.utils.CpachaUtil;
import com.ccut.lc.utils.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("system")
public class UserController {
    @Resource
    private IUserService userService;
    /**
     * 登陆页面
     * @param model
     * @return
     */
    @RequestMapping(value = "/login",method=RequestMethod.GET)
    public ModelAndView login(ModelAndView model){
        model.setViewName("system/login");
        return model;
    }

    @RequestMapping("welcome")
    private String welcome(){
        return "welcome";
    }

    /**
     * 系统住界面
     * @return
     */
    @RequestMapping("home")
    private String home(){
        return "system/main";
    }
    /**
     * 注销登录
     * @param request
     * @return
     */
    @RequestMapping(value = "/loginOut",method=RequestMethod.GET)
    public String loginOut(HttpServletRequest request){
        request.getSession().setAttribute("loginUser", null);
        return "redirect:login";
    }
    /**
     * 登录表单提交
     * @return
     */
    @RequestMapping("login1")
    @ResponseBody
    public Map<String, String> login1(String username, String password, String vcode, String type, HttpServletRequest request){
        Map<String, String> ret = new HashMap<String, String>();
        if(StringUtils.isEmpty(username)){
            ret.put("type", "error");
            ret.put("msg", "用户名不能为空!");
            return ret;
        }
        if(StringUtils.isEmpty(password)){
            ret.put("type", "error");
            ret.put("msg", "密码不能为空!");
            return ret;
        }
        if(StringUtils.isEmpty(vcode)){
            ret.put("type", "error");
            ret.put("msg", "验证码不能为空!");
            return ret;
        }
        String loginCpacha = (String)request.getSession().getAttribute("loginCpacha");
        if(StringUtils.isEmpty(loginCpacha)){
            ret.put("type", "error");
            ret.put("msg", "长时间未操作，会话已失效，请刷新后重试!");
            return ret;
        }
        if(!vcode.toUpperCase().equalsIgnoreCase(loginCpacha.toUpperCase())){
            ret.put("type", "error");
            ret.put("msg", "验证码错误!");
            return ret;
        }
        request.getSession().setAttribute("loginCpacha", null);
        User ul = userService.login(username ,password, type);
        if (ul==null) {
            ret.put("type", "error");
            ret.put("msg", "用户不存在或密码错误");
            return ret;
        }
        request.getSession().setAttribute("userType", type);
        request.getSession().setAttribute("loginUser", ul);
        ret.put("type", "success");
        ret.put("msg", "登录成功!");
        return ret;
    }
    /**
     * 显示 验证码
     * @param request
     * @param vl
     * @param w
     * @param h
     * @param response
     */
    @RequestMapping(value="/get_cpacha",method= RequestMethod.GET)
    public void getCpacha(HttpServletRequest request,
                          @RequestParam(value="vl",defaultValue="4",required=false) Integer vl,
                          @RequestParam(value="w",defaultValue="98",required=false) Integer w,
                          @RequestParam(value="h",defaultValue="33",required=false) Integer h,
                          HttpServletResponse response){
        CpachaUtil cpachaUtil = new CpachaUtil(vl, w, h);
        String generatorVCode = cpachaUtil.generatorVCode();
        request.getSession().setAttribute("loginCpacha", generatorVCode);
        BufferedImage generatorRotateVCodeImage = cpachaUtil.generatorRotateVCodeImage(generatorVCode, true);
        try {
            ImageIO.write(generatorRotateVCodeImage, "gif", response.getOutputStream());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @RequestMapping("list")
    public String selUserList(QueryVo queryVo, Model model){
        Page<User> page = userService.selUserList(queryVo);
        model.addAttribute("page",page);
        return "user/userList";
    }
    @RequestMapping(value = "/addUserUi",method=RequestMethod.GET)
    public ModelAndView addUserUi(ModelAndView model){
        model.setViewName("user/addUser");
        return model;
    }
    @RequestMapping("addUser")
    @ResponseBody
    public String addUser(Product product){
        String msg = "1";
        try {
//            product.setProduct_time(format);
//            productListService.addProduct(product);
            msg = "0";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return msg;
    }
}
